<?php
# 文件名称:controller.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
//控制类
class C_controller extends Base {
	var $_name		= '';
	
	var $_methods 	= null;
	var $_taskMap 	= null;
	
	var $_task 		= null;
	
	var $_doTask 	= null;
	
	function C_controller()
	{	
		$this->_taskMap		= array();
		$this->_methods		= array();
		
		//注册函数
		$thisMethods	= get_class_methods( get_class( $this ) );
		$baseMethods	= get_class_methods( 'C_controller' );
		$methods		= array_diff( $thisMethods, $baseMethods );
		
		$methods[] = 'display';
		// Iterate through methods and map tasks
		foreach ( $methods as $method )
		{
			if ( substr( $method, 0, 1 ) != '_' ) {
				$this->_methods[] = strtolower( $method );
				// auto register public methods as tasks
				$this->_taskMap[strtolower( $method )] = $method;
			}
		}
		$this->registerDefaultTask( 'display' );
		//parent::Base();
		//$this->_sys_initialize();
	}
	/**
	 * 初始化所有基类
	 *
	 * @访问限制	private
	 * @返回类型	void
	 */
	function _sys_initialize()
	{
		/*
		$classes = array(
							'config'	=> 'config',
							'input'		=> 'environment/input',
							'uri'		=> 'environment/uri',
							'router'	=> 'environment/router',
							'lang'		=> 'language/language'
							);		
		foreach ($classes as $var => $class)
		{
			$this->$var =_import($class);
		}		
		*/
		if (floor(phpversion()) >= 5)
		{			
			$this->load =_import('loader');			
			//$this->load->_sys_autoloader();
			
		}
		else
		{
			//php4 Object继承于C_loader类
			//$this->_sys_autoloader();
			
			// 统一php4和php5的调用接口
			foreach (array_keys(get_Object_vars($this)) as $attribute)
			{
				if (is_Object($this->$attribute))
				{
					$this->load->$attribute =& $this->$attribute;
				}
			}
		}		
		
	}
	//显示
	function display($tpl = null)
	{
		$SYS =& get_instance();
		//view
		$view = & $this->getView( );
		
		//model
		if ($model = & $this->getModel()) {
			$view->setModel($model, true);
		}
		ob_start();
		//解析
		$view->display($tpl);		
		$SYS->doc->render();
		$SYS->response->setBody(ob_get_contents());
		@ob_end_clean();
	}
	//获取类名
	function getName()
	{
		$name = $this->_name;
		if (empty( $name ))
		{
			$name = get_class($this);
		}
		return $name;
	}
	//获取model
	function &getModel( $name = '', $prefix = '' )
	{
		if ( empty( $name ) ) {
			$name = $this->getName();
		}
		if ( empty( $prefix ) ) {
			$filename = $name . '.model';
			$model = $name . 'model';
		}else
		{
			$filename = $name . ".$prefix";
			$model = $name . $prefix;
		}
		//SYS php4中为全局变量引用，php5中为静态变量引用
		$SYS =& get_instance();
		$SYS->loader->model($filename);
		$tmp = &$SYS->$model;
		return $tmp;
	}
	
	//获取view
	function &getView( $name = '', $prefix = '')
	{
		if ( empty( $name ) ) {
			$name = $this->getName();
		}
		if ( empty( $prefix ) ) {
			$filename = $name . '.view';
			$view = $name . 'view';
		}else
		{
			$filename = $name . ".$prefix";
			$view = $name . $prefix;
		}
		$SYS =& get_instance();
		$SYS->loader->view($filename);
		$tmp = &$SYS->$view;
		return $tmp;
	}
	function permit($action = null , $return = FALSE) {
		$SYS =& get_instance();
		$SYS->loader->helper('url');
		$my = $SYS->session->get('admin_name');
		if (!$my) {
			//redirect(WEBSELF.'/login');
			//exit();
		}
		if($action == null) return;
		$access = $SYS->access->check($my->id,$action);
		if(!$access && !$return)
		{
			show_js_error(C_text::_('NOACCESS'),WEBSELF.'/sysadmin');
			exit();
		}
		return $access;
	}
	function getTasks()
	{
		return $this->_methods;
	}
	function registerTask( $task, $method )
	{
		if ( in_array( strtolower( $method ), $this->_methods ) ) {
			$this->_taskMap[strtolower( $task )] = $method;
		}
	}
	function getTask()
	{
		return $this->_task;
	}
	//执行任务->函数
	function execute( $task )
	{		
		$SYS = &get_instance();
		$task = $task==''?$SYS->request->getvar('task'):$task;//来自post的task
		$this->_task = $task;		
		$task = strtolower( $task );
		if (isset( $this->_taskMap[$task] )) {
			$doTask = $this->_taskMap[$task];
		} elseif (isset( $this->_taskMap['__default'] )) {
			$doTask = $this->_taskMap['__default'];
		} else {
			return false;
		}
		// 当前任务
		$this->_doTask = $doTask;
		$retval = $this->$doTask();
		return $retval;
	}
	//默认函数
	function registerDefaultTask( $method )
	{
		$this->registerTask( '__default', $method );
	}
	function setLanguageFile( $filename )
	{
		$SYS =&get_instance();
		$tmp = explode(DS,FCPATH);
		array_pop($tmp);
		$is_admin = array_pop($tmp)==$SYS->config->item('administrator_file');
		if($is_admin)
		{
			$SYS->lang->admin_language=$SYS->request->getVar( "admin_language",$SYS->lang->admin_language, 'COOKIE','cmd' );
		}
		$index = $is_admin ?$SYS->lang->admin_language:$SYS->lang->site_language;
		$SYS->doc->setMetaData('Content-Language', $index , true );
		$SYS->request->setVar('languageID',$index.'_'.$filename);		
		$SYS->lang->load($index,TRUE,$is_admin);		
		$SYS->lang->load($index.'_'.$filename,TRUE,$is_admin);
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>